package es.xpt.ysf.flow;

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

/**
 * @author alejandro.villanueva
 */
public class FlowControllerTestWriter {

	Flow flow;
	XStream xstr;

	public void run() {
		configureXStream();
		flow = createTravelioModel();
		System.out.println(xstr.toXML(flow));
		flow = createTestModel();
		System.out.println(xstr.toXML(flow));
	}

	private void configureXStream() {
		xstr = new XStream();
		// xstr.alias("flow", Flow.class);
		// xstr.alias("state", State.class);
		// xstr.alias("action", Action.class);
		// xstr.addImplicitCollection(Flow.class, "states");
		// xstr.addImplicitCollection(State.class, "actions");
		// xstr.useAttributeFor(State.class, "name");
		// xstr.useAttributeFor(Action.class, "name");
	}

	private Flow createTravelioModel() {
		State StateScheduled = new State("Scheduled");
		State StatePending = new State("Pending");
		State StateSending = new State("Sending");
		State StateSent = new State("Sent");
		State StateDelivered = new State("Delivered");
		State StateError = new State("Error");
		State StateStopped = new State("Stopped");
		State StateDeleted = new State("Deleted");

		Flow flow = new Flow(StateScheduled, StateScheduled, StatePending,
				StateSending, StateSent, StateDelivered, StateError,
				StateStopped, StateDeleted);

		Action ActionQueue = new Action("Queue", StatePending, "resend");
		Action ActionSend = new Action("Send", StateSending, "send");
		Action ActionStop = new Action("Stop", StateStopped, "stop");
		Action ActionDelete = new Action("Delete", StateDeleted, "delete");
		Action ActionResend = new Action("Resend", StatePending, "resend");
		Action ActionConfirm = new Action("Confirm", StateDelivered,
				"confirmDelivery");

		StateScheduled.addActions(ActionQueue, ActionStop);
		StatePending.addActions(ActionStop, ActionSend);
		StateSending.addActions((Action) null);
		StateSent.addActions(ActionDelete, ActionConfirm, ActionResend);
		StateDelivered.addActions(ActionDelete, ActionResend);
		StateError.addActions(ActionStop, ActionResend);
		StateStopped.addActions(ActionDelete, ActionResend);
		StateDeleted.addActions((Action) null);

		return flow;
	}

	private Flow createTestModel() {
		State S1 = new State("S1");
		State S2 = new State("S2");
		List<State> states = new ArrayList<State>();
		states.add(S1);
		states.add(S2);
		Action A1 = new Action("A1", S2, "A1_action");
		Action A2 = new Action("A2", S2, "A2_action");
		S1.addActions(A1, A2);
		return new Flow(S1, states);
	}

	public static void main(String[] args) {
		FlowControllerTestWriter test = new FlowControllerTestWriter();
		test.run();
	}

}
